package com.ferdict;

import java.io.File;

import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

// http://deltafalcon.com/2010/04/mounting-an-android-emulator-sd-card-image-in-windows/
// mounting sdcard to preload photos

public class UploadActivity extends Activity implements OnClickListener {
	private static final String tag = "UploadActivity";

	private String authToken;
	private String[] photos;
	private int curIndex;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.upload);

		authToken = getIntent().getStringExtra("token");
		getMyPhotos();
		updatePhoto();

		findViewById(R.id.upload_prev_button_id).setOnClickListener(this);
		findViewById(R.id.upload_next_button_id).setOnClickListener(this);
		findViewById(R.id.do_upload_button_id).setOnClickListener(this);
		findViewById(R.id.cancel_upload_button_id).setOnClickListener(this);

	}

	void updatePhoto() {
		((ImageView) findViewById(R.id.image_view_id_your_photos))
				.setImageBitmap(BitmapFactory.decodeFile(Constants.PICTURES_DIR
						+ photos[curIndex]));
	}

	void getMyPhotos() {
		try {
			File f = new File(Constants.PICTURES_DIR);
			photos = f.list();
			curIndex = 0;
		} catch (Exception e) {
			Log.e(tag, "Could not get a list of outfits!", e);
		}
	}

	// @Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.upload_prev_button_id:
			if (curIndex >= photos.length - 1) {
				Toast.makeText(this, "No more photos.", Toast.LENGTH_SHORT)
						.show();
			} else {
				curIndex++;
				updatePhoto();
			}
			break;
		case R.id.upload_next_button_id:
			if (curIndex <= 0) {
				Toast.makeText(this, "No more photos.", Toast.LENGTH_SHORT)
						.show();
			} else {
				curIndex--;
				updatePhoto();
			}
			break;
		case R.id.cancel_upload_button_id:
			finish();
			break;
		case R.id.do_upload_button_id:
			String name = ((EditText) findViewById(R.id.name_edit_text_id))
					.getText().toString();
			if (null == name || name.length() == 0) {
				Toast.makeText(UploadActivity.this,
						"You need to name your outfit.", Toast.LENGTH_SHORT)
						.show();
			} else {
				try {
					// Bitmap bm =
					// BitmapFactory.decodeFile(Constants.PICTURES_DIR
					// + photos[curIndex]);
					// ByteArrayOutputStream bos = new ByteArrayOutputStream();
					// bm.compress(CompressFormat.JPEG, 75, bos);
					// byte[] data = bos.toByteArray();
					// String dataStr = Base64
					// .encodeToString(data, Base64.DEFAULT);
					//
					// ArrayList<NameValuePair> nameValuePairs = new
					// ArrayList<NameValuePair>();
					// nameValuePairs.add(new BasicNameValuePair("name", name));
					// nameValuePairs.add(new BasicNameValuePair("token",
					// authToken));
					// nameValuePairs
					// .add(new BasicNameValuePair("photo", dataStr));
					MultipartEntity e = new MultipartEntity();
					e.addPart("name", new StringBody(name));
					e.addPart("token", new StringBody(authToken));
					e.addPart("photo", new FileBody(new File(
							Constants.PICTURES_DIR + photos[curIndex])));
					RestClient.doPost(Constants.SERVER, Constants.PORT,
							Constants.OUTFITS_URL /*
												 * + "&token=" + authToken +
												 * "&name=" + name
												 */, e);

					((EditText) findViewById(R.id.name_edit_text_id))
							.setText("");

					Toast.makeText(
							UploadActivity.this,
							String.format(
									"Outfit(name='%s') photo uploaded.",
									((EditText) findViewById(R.id.name_edit_text_id))
											.getText()), Toast.LENGTH_SHORT)
							.show();
				} catch (Exception e) {
					Log.e(tag, "Could not upload photo!", e);
					Toast.makeText(UploadActivity.this,
							"Could not upload the photo.", Toast.LENGTH_SHORT)
							.show();
				}
				break;
			}
		}
	}
}